/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AreaMapper;
import com.ybkj.daijia.server.mapper.ChargeStartDistanceMapper;
import com.ybkj.daijia.server.mapper.ChargeStartNewWeightMapper;
import com.ybkj.daijia.server.mapper.ChargeStartTimeMapper;
import com.ybkj.daijia.server.mapper.CompanyMapper;
import com.ybkj.daijia.server.mapper.MemberMapper;
import com.ybkj.daijia.server.model.Area;
import com.ybkj.daijia.server.model.AreaWeight;
import com.ybkj.daijia.server.model.ChargeStartDistance;
import com.ybkj.daijia.server.model.ChargeStartNewWeight;
import com.ybkj.daijia.server.model.ChargeStartTime;
import com.ybkj.daijia.server.model.ChargeStartWeight;
import com.ybkj.daijia.server.model.ChargeWeightDistance;
import com.ybkj.daijia.server.service.AreaService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultAreaService implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private ChargeStartTimeMapper chargeStartTimeMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ChargeStartNewWeightMapper chargeStartNewWeightMapper;

    @Autowired
    private ChargeStartDistanceMapper chargeStartDistanceMapper;

    public Page<Area> queryAll(String name) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        if (StringUtils.isNotEmpty(name)) {

            List<Area> content = areaMapper
                .selectByNameLike("%" + name + "%", Pager.getOffset(), Pager.getLimit());
            long total = areaMapper.countByNameLike("%" + name + "%");

            return new PageImpl<Area>(content, pageable, total);
        } else {
            List<Area> content = areaMapper
                .selectByNameLike(null, Pager.getOffset(), Pager.getLimit());
            long total = areaMapper.countByNameLike(null);

            return new PageImpl<Area>(content, pageable, total);
        }

    }

    @Override
    public List<Area> listAll() {

        return areaMapper.selectAll(null, null);
    }

    @Override
    public List<Area> listAllByCompanyId(String name, Long companyId) {
        return areaMapper.selectAllByCompanyId(name, companyId);
    }

    @Override
    public List<Area> listByName(String name, Long companyId) {
        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        } else {
            name = null;
        }
        return areaMapper.selectAll(name, companyId);
    }

    @Transactional(rollbackFor = Exception.class)
    public Area save(Area area) {

        areaMapper.insertRecord(area);

        if (null != area.getChargeStartTimes()) {
            for (ChargeStartTime cst : area.getChargeStartTimes()) {
                if (!cst.isEmpty()) {
                    cst.setAreaId(area.getId());
                    chargeStartTimeMapper.insertRecord(cst);

                    List<ChargeStartDistance> distancelist = new LinkedList<ChargeStartDistance>();
                    if (null != cst.getChargeStartDistances()) {
                        for (ChargeStartDistance distance : cst.getChargeStartDistances()) {
                            if (!distance.isEmpty()) {
                                distance.setChargeStartTimeId(cst.getId());
                                distancelist.add(distance);
                            }
                        }
                    }

                    if (!CollectionUtils.isEmpty(distancelist)) {
                        chargeStartDistanceMapper.batchInsertRecord(distancelist);
                    }
                    List<ChargeStartNewWeight> weightlist = new LinkedList<ChargeStartNewWeight>();
                    if (null != cst.getChargeStartNewWeights()) {
                        for (ChargeStartNewWeight weight : cst.getChargeStartNewWeights()) {
                            if (!weight.isEmpty()) {
                                weight.setChargeStartTimeId(cst.getId());
                                weightlist.add(weight);
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(weightlist)) {
                        chargeStartNewWeightMapper.batchInsertRecord(weightlist);
                    }
                }
            }
        }

        return area;
    }

    @Transactional(rollbackFor = Exception.class)
    public Area update(Area area) {
        Area local = areaMapper.selectByPrimaryKey(area.getId());
        // 修改地区名称
        local.setName(area.getName());
        local.setMemo(area.getMemo());
        areaMapper.updateByPrimaryKey(area);
        List<ChargeStartTime> chargeStartTimes = chargeStartTimeMapper.selectByArea(area.getId());
        if (!CollectionUtils.isEmpty(chargeStartTimes)) {
            for (ChargeStartTime cst : chargeStartTimes) {
                chargeStartDistanceMapper.deleteByChargeStartTime(cst.getId());
                chargeStartNewWeightMapper.deleteByChargeStartTime(cst.getId());
            }
        }
        chargeStartTimeMapper.deleteByArea(area.getId());
        if (!CollectionUtils.isEmpty(area.getChargeStartTimes())) {
            for (ChargeStartTime cst : area.getChargeStartTimes()) {
                if (!cst.isEmpty()) {
                    cst.setAreaId(area.getId());
                    chargeStartTimeMapper.insertRecord(cst);
                    List<ChargeStartDistance> distancelist = new LinkedList<ChargeStartDistance>();
                    if (!CollectionUtils.isEmpty(cst.getChargeStartDistances())) {
                        for (ChargeStartDistance distance : cst.getChargeStartDistances()) {
                            if (!distance.isEmpty()) {
                                distance.setChargeStartTimeId(cst.getId());
                                distancelist.add(distance);
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(distancelist)) {
                        chargeStartDistanceMapper.batchInsertRecord(distancelist);
                    }
                    List<ChargeStartNewWeight> weightlist = new LinkedList<ChargeStartNewWeight>();
                    if (!CollectionUtils.isEmpty(cst.getChargeStartNewWeights())) {
                        for (ChargeStartNewWeight weight : cst.getChargeStartNewWeights()) {
                            if (!weight.isEmpty()) {
                                weight.setChargeStartTimeId(cst.getId());
                                weightlist.add(weight);
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(weightlist)) {
                        chargeStartNewWeightMapper.batchInsertRecord(weightlist);
                    }
                }
            }
        }
        return local;
    }

    public Area findOne(Long id) {

        if (null == id) {
            return null;
        }

        return areaMapper.selectByPrimaryKey(id);
    }

    public List<Area> findByIdIn(Long[] ids) {

        return areaMapper.selectByIdIn(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Area area) {

        // 删除时间段里程计费
        chargeStartDistanceMapper.deleteByArea(area.getId());
        // 删除时间段重量计费
        chargeStartNewWeightMapper.deleteByArea(area.getId());
        // 删除时间段
        chargeStartTimeMapper.deleteByArea(area.getId());
        // 删除公司收费关联
        companyMapper.deleteAreaRelation(area.getId());
        // 删除公司收费关联
        companyMapper.deleteAreaCarRelation(area.getId());
        // 删除客户收费关联
        // memberMapper.deleteAreaRelation(area.getId());
        // 删除客户收费关联
        // memberMapper.deleteAreaCarRelation(area.getId());

        // 删除地区信息
        areaMapper.deleteByPrimaryKey(area.getId());

    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteChargeStartTimesByArea(Long areaId) {
        // 删除时间段里程计费
        chargeStartDistanceMapper.deleteByArea(areaId);
        // 删除时间段
        chargeStartTimeMapper.deleteByArea(areaId);
    }

    @Override
    public Area findByName(String name) {
        return areaMapper.selectByName(name);
    }

    @Override
    public Area findOneWithCharge(Long areaId) {
        if (null == areaId) {
            return null;
        }
        return areaMapper.selectByPrimaryKeyWithCharge(areaId);
    }

    @Override
    public Area findDaijiaByPassenger(Long passengerId) {
        return areaMapper.selectDaijiaByPassenger(passengerId);
    }

    @Override
    public Area findZhuancheByPassenger(Long passengerId) {
        return areaMapper.selectZhuancheByPassenger(passengerId);
    }

    @Override
    public Area findPaotuiByPassenger(Long passengerId) {
        return areaMapper.selectPaotuiByPassenger(passengerId);
    }

    @Override
    public Area findDaijiaByEnterprise(Long enterpriseId) {
        return areaMapper.selectDaijiaByEnterprise(enterpriseId);
    }

    @Override
    public Area findZhuancheByEnterprise(Long enterpriseId) {
        return areaMapper.selectZhuancheByEnterprise(enterpriseId);
    }

    @Override
    public Area findPaotuiByEnterprise(Long enterpriseId) {
        return areaMapper.selectPaotuiByEnterprise(enterpriseId);
    }

    @Override
    public List<Area> queryByName(String name) {
        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        }
        return areaMapper.queryByName(name);
    }

    @Override
    public List<AreaWeight> listAllWeight() {

        return areaMapper.listAllWeight(null);
    }

    @Override
    public List<AreaWeight> listWeightData(String name) {

        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        } else {
            name = null;
        }
        return areaMapper.listAllWeight(name);
    }

    @Override
    public void updateWeight(AreaWeight area) {
        AreaWeight local = areaMapper.selectWeightByPrimaryKey(area.getId());

        // 修改收费名称
        local.setName(area.getName());
        local.setMemo(area.getMemo());
        areaMapper.updateWeightByPrimaryKey(area);

        List<ChargeStartWeight> chargeStartWeight = chargeStartTimeMapper
            .selectByAreaWeight(area.getId());

        if (!CollectionUtils.isEmpty(chargeStartWeight)) {
            for (ChargeStartWeight cst : chargeStartWeight) {
                chargeStartDistanceMapper.deleteByChargeStartWeight(cst.getId());
            }
        }

        chargeStartTimeMapper.deleteByAreaWeight(area.getId());

        if (!CollectionUtils.isEmpty(area.getChargeStartWeight())) {
            for (ChargeStartWeight cst : area.getChargeStartWeight()) {
                if (!cst.isEmpty()) {

                    cst.setAreaId(area.getId());
                    chargeStartTimeMapper.insertWeightRecord(cst);
                    List<ChargeWeightDistance> distancelist = new LinkedList<ChargeWeightDistance>();
                    if (!CollectionUtils.isEmpty(cst.getChargeWeightDistance())) {
                        for (ChargeWeightDistance distance : cst.getChargeWeightDistance()) {
                            if (!distance.isEmpty()) {
                                distance.setChargeStartWeightId(cst.getId());
                                distancelist.add(distance);
                            }
                        }
                    }

                    if (!CollectionUtils.isEmpty(distancelist)) {
                        chargeStartDistanceMapper.batchInsertWeightRecord(distancelist);
                    }
                }
            }
        }
    }

    @Override
    public void saveWeight(AreaWeight area) {
        areaMapper.insertWeightRecord(area);

        if (null != area.getChargeStartWeight()) {
            for (ChargeStartWeight cst : area.getChargeStartWeight()) {
                if (!cst.isEmpty()) {
                    cst.setAreaId(area.getId());
                    chargeStartTimeMapper.insertWeightRecord(cst);

                    List<ChargeWeightDistance> distancelist = new LinkedList<ChargeWeightDistance>();
                    if (null != cst.getChargeWeightDistance()) {
                        for (ChargeWeightDistance distance : cst.getChargeWeightDistance()) {
                            if (!distance.isEmpty()) {
                                distance.setChargeStartWeightId(cst.getId());
                                distancelist.add(distance);
                            }
                        }
                    }

                    if (!CollectionUtils.isEmpty(distancelist)) {
                        chargeStartDistanceMapper.batchInsertWeightRecord(distancelist);
                    }
                }
            }
        }
    }

    @Override
    public AreaWeight findOneWeight(Long id) {
        if (null == id) {
            return null;
        }

        return areaMapper.selectWeightByPrimaryKey(id);
    }

    @Override
    public void deleteWeight(AreaWeight area) {
        // 删除重量阶段计费
        chargeStartDistanceMapper.deleteByAreaWeight(area.getId());
        // 删除重量阈值信息
        chargeStartTimeMapper.deleteByAreaWeight(area.getId());
        // 删除重量收费信息
        areaMapper.deleteWeightByPrimaryKey(area.getId());
    }

}
